/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package infomanagers;

import com.mycompany.fb.visualization.types.Friend;
import com.mycompany.fb.visualization.types.Town;
import com.restfb.*;
import com.restfb.types.Conversation;
import com.restfb.types.Location;
import com.restfb.types.Page;
import com.restfb.types.Place;
import com.restfb.types.User;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Comparator;
import java.util.TreeMap;
/**
 *
 * @author Mato
 */
public class BasicUserInfoManager
{
    private static final String MY_ACCESS_TOKEN = "BAACEdEose0cBAA7o4QBxWZB5ZAVbS9ZAZBAzniSsGOFGRUHKXo6vAQrxZByjYIoUnTeI3JGqctIgGtba30sSUpC1rzYCVZChMymB5LqXzmVfk8cTTQKI7ZCwgn1JSlGDYXTD5JmFyVABEu2va2CZAJSAETZB0czVaEIK7BNL4TuICnYDcVtETtwbFCv2H7gtdmqYJHtb8PtHvRaKuI3Sx421jdCNMUsjwoDOhKYyMZBsvOsAZDZD";
    private FacebookClient facebookClient;
    private Friend user;
    private Connection<Friend> myFriends;
    private Connection<Conversation> myConversations;
   
    /**
     * basic user info manager
     */

   
    /**
     *
     * @param facebookClient
     * basic user info manager loads the user and all the friends
     */
    public BasicUserInfoManager(FacebookClient facebookClient)
    {
        this.facebookClient = facebookClient;
        user = facebookClient.fetchObject("me", Friend.class);
        myFriends = facebookClient.fetchConnection("me/friends", Friend.class, Parameter.with("fields", "id, name, gender, picture, hometown, location, birthday, picture, first_name, last_name"));
        myConversations = facebookClient.fetchConnection("me/conversations", Conversation.class, Parameter.with(MY_ACCESS_TOKEN, this));
    }
   
    public List<Conversation> getConversations()
    {
        
        List<Conversation> messages = new ArrayList<Conversation>();
        for(List<Conversation> listOfConversations : myConversations)
        {
            
            for(Conversation m : listOfConversations)
            {
                messages.add(m);
            }
        }
        return messages;
    }
    
    public String getUserId()
    {
        return user.getId();
    }
    
    /**
     *
     * @return user's name
     */
    public String getUserName()
    {
        return user.getName();
    }
   
    /**
     *
     * @return user's hometown
     */
    public String getUserHometown()
    {
        return user.getHometownName();
    }
   
    /**
     *
     * @return user's location
     */
    public String getUserLocation()      
    {
        if(user.getLocation() == null)
        {
            return null;
        }
        return user.getLocation().getName();
    }
    /**
     * 
     * @param friend 
     * @return friend's location name
     */
    public String getFriendsLocation(Friend friend)
    {
       if(friend.getLocation() == null)
       {
           return null;
       }
       return friend.getLocation().getName();
    }
   
    /**
     *
     * @return list of friends
     */
    public List<Friend> getFriends()
    {
        List<Friend> friends = new ArrayList<Friend>();
        for(List<Friend> listOfFriends : myFriends)
        {
            for(Friend u : listOfFriends)
            {
                friends.add(u);
            }
        }
        return friends;
    }
   
    /**
     *
     * @return list of male friends
     */
    public List<Friend> getMaleFriends()
    {
        List<Friend> maleFriends = new ArrayList<Friend>();
        for(Friend u : getFriends())
        {
            if(u.getGender() != null && u.getGender().equals("male"))
            {
                maleFriends.add(u);
            }
        }
        return maleFriends;
    }
   
    /**
     *
     * @return list of female friends
     */
    public List<Friend> getFemaleFriends()
    {
        List<Friend> femaleFriends = new ArrayList<Friend>();
        for(Friend u : getFriends())
        {
            if(u.getGender() != null && u.getGender().equals("female"))
            {
                femaleFriends.add(u);
            }
        }
        return femaleFriends;
    }
   
    /**
     *
     * @return places where friends are from
     */
    public Map<Town, Long> getFriendsPlaces()
    {
        Map<Town, Long> locations = new HashMap<Town, Long>();
        for(List<Friend> listOfFriends : myFriends)
        {
            for(Friend u : listOfFriends)
            {
                if(u.getLocation() != null)
                {
                    Page page = facebookClient.fetchObject(u.getLocation().getId(), Page.class);
                    Town place = new Town(page.getId(), u.getLocation().getName(), page.getLocation().getCountry(), page.getLocation().getLongitude(), page.getLocation().getLatitude());
                    if(!locations.containsKey(place))
                    {
                        locations.put(place, 1l);
                    }
                    else
                    {
                        locations.put(place, locations.get(place) +1l );
                    }
                }
            }
        }
        return locations;  
    }
   
    /**
     *
     * @param friend
     * @return list of mutual friends
     */
    public List<Friend> getMutualFriends(Friend friend)
    {
        List<Friend> mutualFriends = new ArrayList<Friend>();
        Connection<Friend> connMutualFriends = facebookClient.fetchConnection("me/mutualfriends/" + friend.getId(), Friend.class, Parameter.with("fields", "id, name, gender, picture, hometown, location, birthday, picture, first_name, last_name"));
        for(List<Friend> listOfUsers : connMutualFriends)
        {
            for(Friend u : listOfUsers)
            {
                mutualFriends.add(u);
            }
        }
        return mutualFriends;
    }
   
    /**
     *
     * @return list of favorite pages
     */
    public List<Page> getPages()
    {
        List<Page> usersPages = new ArrayList<Page>();
        Connection<Page> conn = facebookClient.fetchConnection("/me/accounts", Page.class);
        for(List<Page> listOfPages : conn)
        {
            for(Page u : listOfPages)
            {
                usersPages.add(u);
            }
        }
        return usersPages;

    }
   
   
   
   
   
}



